package com.neuedu.shop.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.neuedu.shop.entity.Category;
import com.neuedu.shop.entity.Product;
import com.neuedu.shop.service.ProductService;
import com.neuedu.shop.vo.ConditionVO;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Qin
 * @since 2021-04-10
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService service;
    @RequestMapping(value = "/products")
    public String selectAll(ModelMap map){
        List<Product> products = service.selectAll();
        map.addAttribute("products",products);
        return "forward:products.jsp";
    }
    @RequestMapping(value = "/findTheThirdGrade",method = RequestMethod.GET)
    @ResponseBody
    public List<Category> findTheThirdGrade(){
        return service.findTheThirdGrade();
    }
    @RequestMapping(value = "insertProducts",method = RequestMethod.POST)
    public String insertProduct(Product product, @RequestParam("file") CommonsMultipartFile file, HttpServletRequest request){
        System.out.println("商品对象"+product);
        System.out.println("文件对象" + file);
        service.insertProduct(product,file,request);
        return "redirect:products";
    }
    @RequestMapping(value = "/deleteProduct",method = RequestMethod.GET)
    public String deleteProduct(Integer id){
        service.deleteById(id);
        return "redirect:products";
    }
    @RequestMapping(value = "/findByKeyWords",method = RequestMethod.POST)
    public String findByKeyWords(String Keywords,ModelMap map){
        List<Product> products = service.selectList(new EntityWrapper<Product>().like("product_name" ,Keywords)
        .or()
        .like("product_description",Keywords));
        map.addAttribute("products",products);
        return "forward:products.jsp";
    }
    @RequestMapping(value = "/complexSearch",method = RequestMethod.POST)
    public String complexSearchByCondition(ConditionVO vo,ModelMap map){
        System.out.println(vo);
        List<Product> products = service.complexSearchByCondition(vo);
        map.addAttribute("products",products);
        return "forward:products.jsp";
    }
}
